package com.example.administrator.u17.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.u17.R;
import com.example.administrator.u17.adapter.PaihangFragmentPagerAdapter;
import com.example.administrator.u17.fragment.ManHuaInfoFragment;
import com.example.administrator.u17.fragment.OtherFragment;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class InfoActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {

    private TabLayout tab;
    private ViewPager vp;
    private String[] titles = {"漫画详情", "其他作品"};
    private List<Fragment> fragmentList;
    private PaihangFragmentPagerAdapter mAdapter;
    private String path;
    private ImageView iv;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_info);
        initView();
        initData();
        setData();
        setlistener();
    }

    private void setlistener() {
        tab.addOnTabSelectedListener(this);
    }

    private void setData() {
        vp.setAdapter(mAdapter);
        tab.setupWithViewPager(vp);
        tab.setMinimumWidth(50);
        tab.setTabTextColors(Color.BLACK, Color.GREEN);
        tab.setSelectedTabIndicatorColor(Color.GREEN);
        tab.setSelectedTabIndicatorHeight(5);
        Picasso.with(this).load(path).into(iv);
    }

    private void initData() {
        fragmentList = new ArrayList<>();
        ManHuaInfoFragment infoFragment = new ManHuaInfoFragment();
        Intent intent = getIntent();
        if (intent == null) {
            Toast.makeText(this, "kong", Toast.LENGTH_SHORT).show();
        } else {
            path = intent.getStringExtra("path");
            tv.setText(intent.getStringExtra("name"));
        }
        Bundle bundle = new Bundle();
        bundle.putString("text", getIntent().getStringExtra("text"));
        bundle.putString("cate_id", getIntent().getStringExtra("cate_id"));
        bundle.putString("count", getIntent().getStringExtra("count"));
        bundle.putString("last_update_week", getIntent().getStringExtra("last_update_week"));
        bundle.putString("sb", getIntent().getStringExtra("sb"));
        infoFragment.setArguments(bundle);
        fragmentList.add(infoFragment);
        OtherFragment otherFragment = new OtherFragment();
        Bundle bundle1 = new Bundle();
        bundle1.putString("id", getIntent().getStringExtra("id"));
        otherFragment.setArguments(bundle1);
        fragmentList.add(otherFragment);
        mAdapter = new PaihangFragmentPagerAdapter(getSupportFragmentManager(), fragmentList, titles);
    }

    private void initView() {
        tab = (TabLayout) findViewById(R.id.tab_info_activity_1);
        vp = (ViewPager) findViewById(R.id.vp_info_activity_1);
        iv = (ImageView) findViewById(R.id.iv_info_activity_2);
        tv = (TextView) findViewById(R.id.tv_info_activity_name);
    }


    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        for (int i = 0; i < this.tab.getTabCount(); i++) {
            if (this.tab.getTabAt(i) == tab) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                vp.setCurrentItem(i);
                transaction.addToBackStack(null);
                transaction.commit();
                break;
            }
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
    }

    public void click(View view) {
        finish();
    }
}
